package com.avic.rong.userOrder.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.userOrder.event.InformUpdate;
import com.avic.rong.userOrder.activity.MineOrderActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

/**
 * Created by XinEr on 2019/8/22.
 * Describe:
 */
public class OrderTypeSelectAdapter extends RecyclerView.Adapter<OrderTypeSelectAdapter.ViewHolder> {
    private Context context;
    private ArrayList<String> typeList;

    public int mSelectedPos=-1;//保存当前选中的position
    private String orderType;
    public OrderTypeSelectAdapter(Context context, ArrayList<String> typeList,String orderType) {
        this.context = context;
        this.typeList = typeList;
        this.orderType=orderType;
    }
    public void setmSelectedPos(int mSelectedPos) {
        this.mSelectedPos = mSelectedPos;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.order_type_select_item, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        if (!TextUtils.isEmpty(orderType)){
            if (orderType.equals(typeList.get(position))){

                holder.tv_ordertype_select.setBackground(context.getResources().getDrawable(R.drawable.common_main_jianbian_r20_green));
                holder.tv_ordertype_select.setTextColor(context.getResources().getColor(R.color.white));
            }
        }
        if (mSelectedPos==position){
            holder.tv_ordertype_select.setBackground(context.getResources().getDrawable(R.drawable.common_main_jianbian_r20_green));
            holder.tv_ordertype_select.setTextColor(context.getResources().getColor(R.color.white));
        }else if (mSelectedPos!=-1){
            holder.tv_ordertype_select.setBackground(context.getResources().getDrawable(R.drawable.shape_r20_s1_c1c1c1));
            holder.tv_ordertype_select.setTextColor(context.getResources().getColor(R.color.base_title_color));
        }
        holder.tv_ordertype_select.setText(typeList.get(position));
        holder.tv_ordertype_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if(mSelectedPos!=position){//当前选中的position和上次选中不是同一个position 执行
//                    holder.tv_tag_way.setRJCPSelected(true);
//                    holder.tv_tag_way.setBackground(context.getResources().getDrawable(R.drawable.shape_r20_s1_f4f9e7));
//                    if(mSelectedPos!=-1){//判断是否有效 -1是初始值 即无效 第二个参数是Object 随便传个int 这里只是起个标志位
//                        notifyItemChanged(mSelectedPos,0);
//                    }
//                    mSelectedPos=position;
//                }
                MineOrderActivity.tv_title.setText(typeList.get(position));
                setmSelectedPos(position);
                notifyDataSetChanged();

                InformUpdate informUpdate = new InformUpdate();
                informUpdate.setEventType(InformUpdate.ORDER_TYPE_INFORM);
                informUpdate.setOrderType(typeList.get(position));
                EventBus.getDefault().post(informUpdate);

            }
        });
    }

    @Override
    public int getItemCount() {
        return typeList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_ordertype_select;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_ordertype_select=itemView.findViewById(R.id.tv_order_type);
        }
    }
}
